//
//  JQSettingSwitchCell.m
//  JQKit
//
//  Created by JQZ on 2017/12/29.
//  Copyright © 2017年 JQZ. All rights reserved.
//

#import "JQSettingSwitchCell.h"
#import "JQSwtichSettingItem.h"
#import <Masonry/Masonry.h>

@interface JQSettingSwitchCell ()

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UISwitch *cellSwitch;

@end

@implementation JQSettingSwitchCell

@synthesize item = _item;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
        [self setAccessoryView:self.cellSwitch];
        [self.contentView addSubview:self.titleLabel];
        [self p_addMasonry];
    }
    return self;
}

#pragma mark - JQSettingCellProtocol
- (void)setItem:(JQSwtichSettingItem *)item
{
    _item = item;
    [self.titleLabel setText:item.title];
    [self.cellSwitch setOn:item.isOn];
}

#pragma mark - Event Response -
- (void)switchChangeStatus:(UISwitch *)sender
{
    JQSwtichSettingItem * item = (JQSwtichSettingItem *)self.item;
    item.isOn = sender.on;
    if (item.itemOpertion){
        item.itemOpertion(self, item);
    }
}

#pragma mark - Private Methods -
- (void)p_addMasonry
{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).mas_offset(15);
        make.right.mas_lessThanOrEqualTo(self.contentView).mas_offset(-15);
    }];
}


#pragma mark - Getter -
- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
    }
    return _titleLabel;
}

- (UISwitch *)cellSwitch
{
    if (_cellSwitch == nil) {
        _cellSwitch = [[UISwitch alloc] init];
        [_cellSwitch addTarget:self action:@selector(switchChangeStatus:) forControlEvents:UIControlEventValueChanged];
    }
    return _cellSwitch;
}

@end
